<?php

use App\Http\Controllers\AuthController;
use App\Http\Controllers\BlogController;
use App\Http\Controllers\ClassifyController;
use App\Http\Controllers\CommentController;
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

/**
 * 鉴权管理
 */
Route::prefix('auth/')->group(function () {
   Route::post('register', [AuthController::class, 'register']);
   Route::post('login', [AuthController::class, 'login']);
   Route::get('unauthorized', [AuthController::class, 'unauthorized'])->name('unauthorized');
   Route::middleware('auth:api')->group(function () {
       Route::get('me', [AuthController::class, 'me']);
       Route::put('logout', [AuthController::class, 'logout']);
   });
});

/**
 * 博客管理
 */
Route::prefix('blogs')->group(function () {
    Route::middleware('auth:api')->group(function () {
        Route::get('deleted', [BlogController::class, 'getDeletedBlog']);
        Route::post('', [BlogController::class, 'create']);
        Route::delete('soft/{id}', [BlogController::class, 'deleteSoft']);
        Route::get('restore/{id}', [BlogController::class, 'restore']);
        Route::put('{id}', [BlogController::class, 'update']);
    });
    Route::get('search/{keyword}', [BlogController::class, 'search']);
    Route::get('classify/{classify_id}', [BlogController::class, 'getClassifyBlog']);
    Route::get('/user/{id}', [BlogController::class, 'getUsersBlog']);
    Route::get('{id}', [BlogController::class, 'getBlog']);
    Route::get('', [BlogController::class, 'getAll']);
});

/**
 * 用户管理
 */
Route::prefix('users')->group(function () {
    Route::post('nopwd', [UserController::class, 'noPwdRegister']);
    Route::get('exist/{email}', [UserController::class, 'checkUserExist']);
    Route::middleware('auth:api')->group(function () {
        Route::get('{id}', [UserController::class, 'getUser']);
        Route::get('', [UserController::class, 'getAll']);
        Route::post('admin', [UserController::class, 'addAdminUser']);
        Route::put('{id}', [UserController::class, 'editUser']);
        Route::put('level/{id}', [UserController::class, 'changeLevel']);
        Route::post('password/check', [UserController::class, 'checkPassword']);
        Route::put('password/{id}', [UserController::class, 'changePassword']);
        Route::delete('{id}', [UserController::class, 'remove']);
        Route::delete('disable/{id}', [UserController::class, 'disable']);
    });
});

/**
 * 评论模块
 */
Route::prefix('comments')->group(function () {
    // 发布评论 POST /comments/{target}
    Route::post('{target}', [CommentController::class, 'pushComment']);
    // 获得无层级的评论列表
    Route::get('all/{target}', [CommentController::class, 'getNoFormatBlogComment']);
    // 获取评论列表 GET /comments/{target}
    Route::get('{target}', [CommentController::class, 'getBlogComment']);
    // 删除评论 DELETE /comments/{id}
    Route::middleware('auth:api')->delete('{id}', [CommentController::class, 'deleteComment']);
    // 修改评论状态 PUT /comments/status/{id}
    Route::middleware('auth:api')->put('status/{id}', [CommentController::class, 'statusChange']);
});

/**
 * 分类管理
 */
Route::prefix('classify')->group(function () {
    // 获取全部分类 GET /classify
    Route::get('', [ClassifyController::class, 'getAll']);
    // 新建一个分类 POST /classify
    Route::middleware('auth:api')->post('', [ClassifyController::class, 'insert']);
    // 删除一个分类 DELETE /classify/{id}
    Route::middleware('auth:api')->delete('{id}', [ClassifyController::class, 'delete']);
});
